#ifndef PEERNODESERVERUI_H
#define PEERNODESERVERUI_H

#include <QMainWindow>
#include <QWidget>
#include <QListWidget>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QProgressBar>
#include <QGridLayout>
#include <QString>
#include <QTableWidget>
#include <QList>
#include <QTimer>

#include <QtNetwork/QHostAddress>
#include <QtNetwork/QTcpSocket>

#include "peer_node_client_role.h"
#include "peer_node_server_role.h"


class PeerNodeServerUi : public QMainWindow
{
    Q_OBJECT

public:
    PeerNodeServerUi(QWidget *parent = nullptr);
    ~PeerNodeServerUi();

    /// 所有的消息类型
    enum MsgKind{
        NAMEMSG = 0,      // "NM",
        METAMSG = 1,      // "MM",
        DISCONNECT = 2,   // "DC",
        SHAREMSG = 3,     // "SM",
        DELETEMSG = 4,    // "DM",
        SEARCHMSG = 5,    // "SR",
        SIGNMSG = 6,      // "GM",
        OTHERMSG = 7      // "OT",
    };

    static std::string MsgKindStr(enum MsgKind kind)
    {
        switch (kind)
        {
        case NAMEMSG:
            return "NAMEMSG";
        case METAMSG:
            return "METAMSG";
        case DISCONNECT:
            return "DISCONNECT";
        case SHAREMSG:
            return "SHAREMSG";
        case DELETEMSG:
            return "DELETEMSG";
        case SEARCHMSG:
            return "SEARCHMSG";
        case SIGNMSG:
            return "SIGNMSG";
        case OTHERMSG:
            return "OTHERMSG";
        default :
            return "";
        }
    }

private:
    QWidget *myWidget;

    QListWidget *ContentListWidget;

    QLabel *UserNameLabel;
    QLineEdit *UserNameLineEdit;

    QLabel *UserPasswdLabel;
    QLineEdit *UserPasswdLineEdit;

    QLabel *ServerIPLabel;
    QLineEdit *ServerIPLineEdit;

    QLabel *PortLabel;
    QLineEdit *PortLineEdit;

    QPushButton *EnterBtn;
    QPushButton *SignBtn;

    QTableWidget *ResultTableWidget;
    QLineEdit *SearchLineEdit;
    QPushButton *SearchBtn;
    QProgressBar *ProgressBar;
    QPushButton *DownloadBtn;
    QTimer *timer;

    QTableWidget *ShareTableWidget;
    QPushButton *FlushBtn;
    QPushButton *DisableBtn;
    QPushButton *ShareBtn;

    QLineEdit *DirLineEdit;
    QPushButton *SelectDirBtn;

    QGridLayout *layout1;
    QGridLayout *layout2;
    QGridLayout *layout3;
    QGridLayout *mainLayout;

    bool status;
    int MetaServerPort;
    QHostAddress *MetaServerIP;
    QString UserName;
    QTcpSocket *tcpSocket;
    PeerNodeServerRole *p2pserver;
    QList<PeerNodeClientRole*> tasks;/// p2P的任务 列表

    QString ClientDir;

    void FormatMsg(QString &msg, MsgKind kind);
    void UpdateShareTableWidget(QString &msg);
    void UpdateResultTableWidget(QString &msg);

public slots:
    void slotEnter();
    void slotSendMeta();
    void loginConnected();      /// 主动接中心服务器,
    void loginDisconnected();
    void loginDataReceived();
    void slotFlushShare();
    void slotDeleteShare();
    void slotSearch();
    void slotSelectDir();
    void slotDownload();

    void slotSignUp();
    void SignConnected();
    void SocketErrorHandle(QAbstractSocket::SocketError sockErr);
    void SignDisconnected();
    void SignDataReceived();

    void timeout();
};
#endif // PEERNODESERVERUI_H
